package com.Monica.pojo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

public class JdbcServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        request.setCharacterEncoding("utf-8");
        Connection conn = null;
        PreparedStatement sta = null;
        ResultSet res = null;
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        try {
            conn = JDBCUtil.getConnection();
            String sql = "select * from t_user where username=? and password=?";
            sta = conn.prepareStatement(sql);
            sta.setString(1,username);
            sta.setString(2,password);
            res = sta.executeQuery();
            if (res.next()){
                response.getWriter().print("登录成功");
                response.getWriter().print("用户名："+username);
                response.getWriter().print("密码："+password);
            }else {
                response.getWriter().print("登录失败!");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            if (res!=null){
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (sta!=null){
                try {
                    sta.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (conn!=null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
